#include "textwidget.h"
#include <QGroupBox>
#include <QHBoxLayout>
#include <QLabel>
#include <QPushButton>
#include <QEvent>
#include <QClipboard>
#include <QGuiApplication>
#include <typeinfo>
#include "widget_encoder.h"

TextWidget::TextWidget(QWidget *parent)
        : QWidget{parent} {
    container = new QVBoxLayout(this);
    QHBoxLayout *scaleLayout = new QHBoxLayout();
    QLabel *scaleLabel = new QLabel(this);
    scaleLabel->setText("scale:");
    scaleLayout->addWidget(scaleLabel);
    scaleLineEdit = new QLineEdit(this);
    scaleLineEdit->setText("0.5");
    scaleLayout->addWidget(scaleLineEdit, 1);
    container->addLayout(scaleLayout);

    container->addSpacing(20);

    QGroupBox *settingBox = new QGroupBox(this);
    settingBox->setTitle("Setting");
    QHBoxLayout *settingLayout = new QHBoxLayout(settingBox);
    fontFamilyCheckbox = new QCheckBox(this);
    fontFamilyCheckbox->setText("FontFamily");
    settingLayout->addWidget(fontFamilyCheckbox);
    container->addWidget(settingBox);
    container->addStretch();
}

bool TextWidget::isFontFamilyChecked() {
    return fontFamilyCheckbox->isChecked();
}

float TextWidget::getScale() {
    QString text = scaleLineEdit->text();
    if (text.isEmpty()) {
        return 0.0;
    }
    return text.toFloat();
}

TextPopup::TextPopup(QWidget *parent, dymy::Text *textPtr) : QWidget(parent), textPtr(textPtr), type(kCancel) {
    QHBoxLayout *container = new QHBoxLayout(this);


    QPushButton *copyText = new QPushButton(this);
    copyText->setText("Copy");
    QPushButton *copyTextStyle = new QPushButton(this);
    copyTextStyle->setText("CopyStyle");
    QPushButton *continuee = new QPushButton(this);
    continuee->setText("Continue");
    QPushButton *cancel = new QPushButton(this);
    cancel->setText("Cancel");
    container->addWidget(copyText);
    container->addWidget(copyTextStyle);
    container->addWidget(continuee);
    container->addWidget(cancel);
    clipboard = QGuiApplication::clipboard();
    connect(clipboard, &QClipboard::dataChanged, this, &TextPopup::on_clipboard_datachanged);
    connect(copyText, &QPushButton::clicked, [=](bool checked = false) {
        this->type = kCopyText;
        this->code = QString::fromStdString(textPtr->generateCode());
        QGuiApplication::clipboard()->setText(this->code);
    });
    connect(copyTextStyle, &QPushButton::clicked, [=](bool checked = false) {
        this->type = kCopyStyle;
        this->code = QString::fromStdString(textPtr->generateTextStyle());
        QGuiApplication::clipboard()->setText(this->code);
    });
    connect(continuee, &QPushButton::clicked, [=](bool checked = false) {
        this->type = kContinue;
        close();
    });
    connect(cancel, &QPushButton::clicked, [=](bool checked = false) {
        this->type = kCancel;
        close();
    });
}

void TextPopup::on_clipboard_datachanged() {
    if (this->code == clipboard->text()) {
        close();
    }
}

void TextPopup::closeEvent(QCloseEvent *event) {
    emit onClosed(type);
}

TextPopup::~TextPopup() {
}
